/*
 * Copyright (C) 2018 Zhengzhou grandtech Technology CO.,LTD.
 * All rights reserved.
 * Official Web Site: http://www.grandtech.com.
 * Developer Web Site: http://open.grandtech.com.
 */

package com.grandtech.oss.api;

import cn.hutool.core.lang.PatternPool;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.grandtech.oss.common.pojo.Pagination;
import com.grandtech.oss.common.pojo.RestfulMessage;
import com.grandtech.oss.module.model.OSSDeveloper;
import com.grandtech.oss.service.OSSDeveloperService;
import com.grandtech.oss.utils.RandomUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/***
 *
 * @since:oss-server 1.0
 * @author <a href="mailto:yanggh@foxmail.com">yanggh@foxmail.com</a> 
 * 2018/09/17 12:01
 */
@RestController
@RequestMapping("/api/v1/oss/developer")
public class DeveloperApplication extends RootApis {
    /**
     * @apiDefine developer developer（开发者管理）
     */

    @Autowired
    OSSDeveloperService ossDeveloperService;

    /***
     * @api {GET} /api/v1/oss/developer/queryByPage queryByPage（获取开发者列表）
     * @apiName queryByPage
     * @apiGroup developer
     * @apiVersion 1.0.0
     *
     * @apiParam {Integer} page 当前页
     * @apiParam {Integer} rows 每页行数
     * @apiParam {String} sortOrder 排序字段
     * @apiParamExample {json} Request-Example：
     * {
     *     "rows": 10,
     *     "page": 1,
     *     "sortOrder": "asc"
     * }
     *
     * @apiSuccess {json} success 返回数据
     * @apiSuccessExample {json} Success-Response:
     * {
     *     "code": 8200,
     *     "count": 2,
     *     "current_page": 1,
     *     "message": null,
     *     "page_size": 10,
     *     "total_page": 1,
     *     "data":[{
     *         "appid": "oss1g5yik",
     *         "appsecret": "a7soma73",
     *         "createTime": "2018-09-10 17:06:19",
     *         "email": "1577388557@qq.com",
     *         "id": "386607787ada4676a52cf78e1b15311a",
     *         "intro": "",
     *         "name": "yanggh",
     *         "status": "0",
     *         "tel": "15600902056",
     *         "useSpace": 8376085,
     *         "useSpaceStr": "7.99MB"
     *     }]
     * }
     */
    @GetMapping("/queryByPage")
    public Pagination<OSSDeveloper> queryByPage(OSSDeveloper ossDeveloper,
                                                @RequestParam(value = "page", defaultValue = "1") Integer current_page,
                                                @RequestParam(value = "rows", defaultValue = "10") Integer page_size) {
        return ossDeveloperService.queryByPage(ossDeveloper, current_page, page_size);

    }

    /***
     * @api {GET} /api/v1/oss/developer/queryById queryById（获取开发者列表）
     * @apiName queryById
     * @apiGroup developer
     * @apiVersion 1.0.0
     *
     * @apiParam {String} id 开发者id
     * @apiParamExample {json} Request-Example：
     * {
     *     "id": "ba505821a51e4126b738307b55e35e8c"
     * }
     *
     * @apiSuccess {json} success 返回数据
     * @apiSuccessExample {json} Success-Response:
     * {
     *     "code": 8200,
     *     "message": null,
     *     "data"：{
     *         "appid": "oss1g5yik",
     *         "appsecret": "a7soma73",
     *         "createTime": "2018-09-10 17:06:19",
     *         "email": "1577388557@qq.com",
     *         "id": "386607787ada4676a52cf78e1b15311a",
     *         "intro": "",
     *         "name": "yanggh",
     *         "status": "0",
     *         "tel": "15600902056",
     *         "useSpace": 8376085,
     *         "useSpaceStr": "7.99MB"
     *     }
     * }
     */
    @PostMapping("/queryById")
    public RestfulMessage queryById(String id) {
        RestfulMessage restfulMessage = new RestfulMessage();
        try {
            //验证邮箱
            assertArgumentNotEmpty(id, "id不能为空");
            restfulMessage.setData(ossDeveloperService.selectById(id));
            successResultCode(restfulMessage);
        } catch (Exception e) {
            restfulMessage = wrapperException(e);
        }
        return restfulMessage;
    }

    /***
     * @api {POST} /api/v1/oss/developer/delete delete（根据应用id删除开发者）
     * @apiName delete
     * @apiGroup developer
     * @apiVersion 1.0.0
     *
     * @apiParam {String} id 开发者id
     * @apiParamExample {json} Request-Example：
     * {
     *     "id": "46c2ec51380e431b96ae55ba35c37f30"
     * }
     *
     * @apiSuccess {json} success 返回数据
     * @apiSuccessExample {json} Success-Response:
     * {
     *     "code": 8200,
     *     "data": null,
     *     "message": "Success"
     * }
     */
    @PostMapping("/delete")
    public RestfulMessage delete(String id) {
        RestfulMessage restfulMessage = new RestfulMessage();
        try {
            //验证邮箱
            assertArgumentNotEmpty(id, "id不能为空");
            String[] ids = StrUtil.split(id, ",");
            ossDeveloperService.deleteBatchIds(Lists.newArrayList(ids));
            successResultCode(restfulMessage);
        } catch (Exception e) {
            restfulMessage = wrapperException(e);
        }
        return restfulMessage;
    }

    /***
     * @api {POST} /api/v1/oss/developer/merge merge（新增或编辑开发者）
     * @apiName merge
     * @apiGroup developer
     * @apiVersion 1.0.0
     *
     * @apiParam {json} ossAppInfo 应用信息
     * @apiParamExample {json} Request-Example：
     * {
     *     "id": "386607787ada4676a52cf78e1b15311a", --新增时为空，修改时非空
     *     "name": "test1",
     *     "email": "test1@qq.com",
     *     "tel": "12345678900",
     *     "intro": "desc" --描述
     * }
     *
     * @apiSuccess {json} success 返回数据
     * @apiSuccessExample {json} Success-Response:
     * {
     *     "code": 8200,
     *     "data": null,
     *     "message": "Success"
     * }
     */
    @PostMapping("/merge")
    public RestfulMessage merge(OSSDeveloper ossDeveloper) {
        RestfulMessage restfulMessage = new RestfulMessage();
        try {
            //验证邮箱
            assertArgumentNotEmpty(ossDeveloper.getName(), "开发者名称不能为空");
            assertArgumentNotEmpty(ossDeveloper.getTel(), "手机号不能为空");
            assertArgumentNotEmpty(ossDeveloper.getEmail(), "邮箱不能为空");
            isTrue(ReUtil.isMatch(PatternPool.EMAIL, ossDeveloper.getEmail()), "邮箱格式非法");
            if (StrUtil.isEmpty(ossDeveloper.getId())) {
                //insert
                ossDeveloper.setAppid("oss" + RandomUtils.random(6));
                ossDeveloper.setAppsecret(RandomUtils.random(8));
                ossDeveloper.setStatus("0");
                ossDeveloper.setCreateTime(DateTime.now().toString("yyyy-MM-dd HH:mm:ss"));
                ossDeveloper.setUseSpace(0L);
                ossDeveloper.setUseSpaceStr("0kb");
                ossDeveloperService.insert(ossDeveloper);
            } else {
                ossDeveloperService.updateById(ossDeveloper);
            }
            successResultCode(restfulMessage);
        } catch (Exception e) {
            restfulMessage = wrapperException(e);
        }
        return restfulMessage;
    }
}
